package com.attendance.www.hr.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.RoleBean;
import com.attendance.www.hr.beans.RolePageBean;
import com.attendance.www.hr.beans.UserEmployeeBean;
import com.attendance.www.hr.constant.AppConstant;
import com.attendance.www.hr.entitys.Role;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IRoleService;
import com.zhangkongli.frame.core.ResultMap;

/**
 * 岗位管理
 * @author 张鑫
 * 2017-10-19
 *
 */
@Controller
@RequestMapping("/role")
public class RoleController {

	@Resource
	private IRoleService roleService;
	
	/**
	 * 新增岗位
	 * @param request
	 * @param roleBean
	 * @return
	 */
	@RequestMapping(value = "/saveRole.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody 
	public Object saveRole(HttpServletRequest request,@RequestBody RoleBean roleBean){
		if (roleBean!=null) {
			if (StringUtils.isBlank(roleBean.getRoleCode())
					|| StringUtils.isBlank(roleBean.getRoleName())
					|| roleBean.getDepartmentId()==null) {
				throw new ResultFaultException("0_00001");
			}
			if (roleService.saveRole(roleBean)) {
				return ResultMap.success();
			}
		}
		throw new ResultFaultException("0_00007");
	}
	
	/**
	 * 逻辑删除岗位
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/deleteRole.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody 
	public Object deleteRole(HttpServletRequest request){
		String roleId=request.getParameter("roleId");
		if (StringUtils.isBlank(roleId)) {
			throw new ResultFaultException("0_00001");
		}
		if (roleService.getVerifyDelete(roleId)) {
			throw new ResultFaultException("0_00017");
		}
		if (roleService.deleteRole(roleId)) {
			return ResultMap.success();
		} else {
			throw new ResultFaultException("0_00009");
		}
	}
	
	/**
	 * 修改岗位信息
	 * @param request
	 * @param roleBean
	 * @return
	 */
	@RequestMapping(value = "/updateRole.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody 
	public Object updateRole(HttpServletRequest request,@RequestBody RoleBean roleBean){
		if (roleBean!=null) {
			if (roleBean.getRoleId()==null||
				StringUtils.isBlank(roleBean.getRoleCode())||
				StringUtils.isBlank(roleBean.getRoleName()))
				throw new ResultFaultException("0_00001");
			if (roleService.updateRole(roleBean)) {
				return ResultMap.success();
			}
		}
		throw new ResultFaultException("0_00008");
	}

	/**
	 * 查询岗位
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getRoleList.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody 
	public Object getRoleList(HttpServletRequest request,@RequestBody RoleBean roleBean){
		Integer pageNo=roleBean.getPageNo();
		Integer pageCount=roleBean.getPageCount();
		if (pageNo==null||pageCount==null) {
			throw new ResultFaultException("0_00001");
		}
		Integer startNum = 0;
		if (pageNo > 1) {
			startNum = (pageNo - 1) * pageCount; 
		}
		RolePageBean rolePageBean=new RolePageBean();
		rolePageBean=roleService.getRoleList(roleBean, startNum, pageCount);
		return ResultMap.success(rolePageBean);
	}
	
	/**
	 * 查询某人的岗位集合
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getRoleListByEmployeeId.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody 
	public Object getRoleListByEmployeeId(HttpServletRequest request){
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		return ResultMap.success(roleService.getRoleListByEmployeeId(userEmployee.getEmployeeId()));
	}
	
	/**
	 * 岗位数据导出
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getRoleExport.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody 
	public Object getRoleExport(HttpServletRequest request) {
		String url = roleService.getRoleExport();
		return ResultMap.success(url);
	}
	
}
